package  com.mongoosehome.web.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * 时间日期处理帮助类
 */
public class DateTimeUtils {
    // 定义月份
    public static final int JANUARY                 = 0;            // 一月
    public static final int FEBRUARY                = 1;            // 二月
    public static final int MARCH                   = 2;            // 三月
    public static final int APRIL                   = 3;            // 四月
    public static final int MAY                     = 4;            // 五月
    public static final int JUNE                    = 5;            // 六月
    public static final int JULY                    = 6;            // 七月
    public static final int AUGUST                  = 7;            // 八月
    public static final int SEPTEMBER               = 8;            // 九月
    public static final int OCTOBER                 = 9;            // 十月
    public static final int NOVEMBER                = 10;           // 十一月
    public static final int DECEMBER                = 11;           // 十二月
    private static final String[] MONTH_CN = new String[]{"一月", "二月", "三月", "四月", "五月", "六月", 
                                                          "七月", "八月", "九月", "十月", "十一月", "十二月"};
    
    // 定义星期
    public static final int SUNDAY                  = 0;            // 星期天
    public static final int MONDAY                  = 1;            // 星期一
    public static final int TUESDAY                 = 2;            // 星期二
    public static final int WEDNESDAY               = 3;            // 星期三
    public static final int THURSDAY                = 4;            // 星期四
    public static final int FRIDAY                  = 5;            // 星期五
    public static final int SATURDAY                = 6;            // 星期六
    private static final String[] WEEK_CN = new String[]{"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
    
    // 定义时间单位
    public static final String TIMEUNIT_MINUTE      = "M";          // 时间单位:分钟
    public static final String TIMEUNIT_HOUR        = "H";          // 时间单位:小时
    public static final String TIMEUNIT_DAY         = "D";          // 时间单位:天
    private static final String[] TIMEUNIT_CN = new String[]{"分钟", "小时", "天"};
    
    // 定义周期单位
    public static final String PERIODUNIT_HOUR      = "hour";       // 周期单位：小时
    public static final String PERIODUNIT_DAY       = "day";        // 周期单位：天
    public static final String PERIODUNIT_WEEK      = "week";       // 周期单位：周
    public static final String PERIODUNIT_MONTH     = "month";      // 周期单位：月
    public static final String PERIODUNIT_YEAR      = "year";       // 周期单位：年
    
    //时间毫秒值
    public static final long DAY_MM = 1000L*60*60*24 ;//一天
    public static final long HOUR_MM = 1000L*60*60 ;//一小时
    private static final String[] PERIODUNIT_CN = new String[]{"小时", "天", "周", "月", "年"};

	public static String getPeriodUnitCN(String periodUnit){
        String[] temp = new String[]{PERIODUNIT_DAY, PERIODUNIT_WEEK, PERIODUNIT_MONTH, PERIODUNIT_YEAR};
        for(int i = 0; i < temp.length; i++){
            if(periodUnit.equalsIgnoreCase(temp[i]))
                return PERIODUNIT_CN[i];
        }
        return periodUnit;
    }
    
    /**
     * 取得当前日期(不含时间)
     * @return
     */
    public static String getCurDate() {
        return formatDate(new Date());
    }
    
    /**
     * 取得当前时间 
     * @return
     */
    public static String getCurDateTime() {
        return formatDateTime(new Date());
    }
    
    /**
     * 将参数的中的时间格式化为：yyyy-MM-dd 
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        if (null == date)
            return "";
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(date);
    }

    /**
     * 将参数的中的时间格式化为：yyyy-MM-dd HH:mm:ss 
     * @param date
     * @return
     */
    public static String formatDateTime(Date date) {
        return formatDateTime(date, "yyyy-MM-dd HH:mm:ss");
    }
    
    /**
     * 将参数的中的时间格式化为：yyyy-MM-dd HH:mm
     * @param date
     * @return
     */
    public static String formatDateTime2Minute(Date date) {
        return formatDateTime(date, "yyyy-MM-dd HH:mm");
    }
    
    /**
     * 将参数的中的时间格式化指定的格式 
     * @param date
     * @param format 格式
     * @return
     */
    public static String formatDateTime(Date date, String format) {
        if (null == date)
            return "";
        DateFormat df = new SimpleDateFormat(format);
        return df.format(date);
    }
    
    /**
     * 取得指定日期中的小时数 
     * @param date
     * @return
     */
    public static int getHour(Date date) {
        if (null == date)
            return 0;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }
    
    /**
     * 取得指定日期中的分钟数 
     * @param date
     * @return
     */
    public static int getMinute(Date date) {
        if (null == date)
            return 0;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }
    
    /**
     * 指定的字符串时间转换成Date 
     * @param dateTime 所要转换的时间
     * @return  
     */
    public static Date getDate(String dateTime) {
        if(null == dateTime || dateTime.length() == 0)  return null;
        DateFormat df = null;
        if(dateTime.length() == "yyyy-MM-dd HH:mm:ss".length()){
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");            
        }else if(dateTime.length() == "yyyy-MM-dd HH:mm".length()){
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        }else if(dateTime.length() == "yyyy-MM-dd".length()){
            df = new SimpleDateFormat("yyyy-MM-dd");
        }else{
            return null;
        }
        
        try {
            return df.parse(dateTime);
        } catch (ParseException pe) {
            return null;
        }
    }
    
    /**
     * 把时间组合成YYYY-MM-DD HH:MM的格式
     * @param date
     * @param hour
     * @param minute
     * @return
     */
    public static String buildDateTime(String date, String hour, String minute){
        if(null == date || date.length() == 0)
            return "";
        
        if(null == hour)
            return date;

        String hm = (hour.length() == 1) ? "0" + hour : hour;
        if(null != minute){
            hm += ":";
            hm += (minute.length() == 1) ? "0" + minute : minute;
        }
        
        return date + " " + hm;
    }
    
    /**
     * 获取指定日期的起始时间(类似yyyy-MM-dd 00:00:00)
     * @param date 指定日期
     * @return 转换后的日期
     */
    public static Date getStartDate(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTime();
    }
    
    /**
     * 获取指定日期的结束时间(类似yyyy-MM-dd 23:59:59)
     * @param date 指定日期
     * @return 转换后的日期
     */
    public static Date getEndDate(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
        return calendar.getTime();
    }
    
    /**
     * 为时间添加一天
     * @param date
     * @return
     */
    public static Date addOneDay(Date date){
    	long times = date.getTime();
    	times+=DateTimeUtils.DAY_MM;
    	Date d= new Date(times);
    	return d;
    }
    
    /**
     * 为时间添加一小时
     * @param date
     * @return
     */
    public static Date addOneHour(Date date){
    	long times = date.getTime();
    	times+=DateTimeUtils.HOUR_MM;
    	Date d= new Date(times);
    	return d;
    }
    
    /*
     * 生成特定时间格式
     */
    public static String formatDate4YYMMDD(long time,String timeformat){
    	Date now = new Date();
    	long dateLong = time*1000;
    	//dateLong=dateLong-(61*1000);	//解决刚刚发表完，显示0分钟前发表的问题
    	dateLong-=(61*1000);	//解决刚刚发表完，显示0分钟前发表的问题
    	Date date = new Date(dateLong);
    	long nowTime = now.getTime();
    	long scrip = nowTime - dateLong;
    	if( scrip <= DAY_MM){
    		if(scrip <= HOUR_MM){
    			return (scrip/(1000*60)) + "分钟前";
    		}else{
    			return (scrip/(1000*60*60)) + "小时前";
    		}
    	}else{
    		if(StringUtils.isFine(timeformat)){
    			return DateTimeUtils.formatDateTime(date,timeformat);
    		}else{
    			return DateTimeUtils.formatDateTime(date,"yyyy年MM月dd日");
    		}
    		
    	}
    }
    
    /*
     * 返回当前时间后的某个时间
     */
    public static Date addCurrentTime(long time){
    	Date now = new Date();
    	if(time<=0){
    		return now;
    	}
    	return new Date(now.getTime()+time);
    }
}
